
var MAP_EDIT_JOB = "/livcos.org/Map/web/edit.job";
var MAP_EDIT_CLIPBOARD_JOB = "/livcos.org/Map/web/edit_clipboard.job";

function ViewSimpleAction() {
}

ViewSimpleAction.prototype.perform = function() {
  window.location = window.location.href.replace("/nav.html", "/iNav.html");
}


function ViewGridAction() {
}

ViewGridAction.prototype.perform = function() {
  window.location = window.location.href.replace("/nav.html", "/grid.html");
}


function ViewDocAction() {
}

ViewDocAction.prototype.perform = function() {
  window.location = window.location.href.replace("/nav.html", "/doc.html");
}


function ShowInsertAction(itemId) {
  this.id = "showInsert";
  this.itemId = itemId;
}

ShowInsertAction.prototype.perform = function() {
  if (!this.itemId && this.entity)
    this.itemId = this.entity.getId();
  this.mapId = this.itemId.substring(0, this.itemId.lastIndexOf("#"));
  var doc = Doc();
  this.panel = doc.itemInsertPanel;
  if (!this.panel) {
    this.panel = new WindowPanel("insert", doc);
    this.panel.setModal(true);
    this.panel.isRememberPosition = false;
    this.panel.header.innerHTML = "Add Item";
    doc.itemInsertPanel = this.panel;
    var p = Dom("_insert");
    this.panel.element.appendChild(p.element);
    p.show();
    var input = p.element.getElementsByTagName("input")[0];
    input._panel = this.panel;
    this.panel._lbInput = input;
    this.panel._list = new Array();
    this.panel._mapId = this.mapId;
    this.panel._loadItemAutoComplete = function(uri) {
		  if ((uri == this._lastMapId) || (uri.indexOf("://") >= 0) || (uri.indexOf("#") >= 0))
		    return;
		  this._lastMapId = uri;
		  var r = new LivCosResource("json/list.view?map=" + this._mapId + "&uri=" + uri, "", StatusDisplay.getInstance());
		  r._list = this._list;
		  r.onSuccess = function() {
		    var l = Resource.parseJsArray(this.text);
		    if (!l) {
		      StatusDisplay.getInstance().showError("Failed to load auto-complete list!", null, true);
		      return;
		    }
		    this._list.length = 0;
		    for (var i = 0; i < l.length; ++i)
		      this._list[i] = l[i];
		    StatusDisplay.getInstance().show("...list loaded.", null, true);
		  }
		  r.load();
		};    
    new AutoCompleteInput(input, this.panel._list);
    new EventListener(input, "focus", function(evn) {
    	var uri = this._panel._uriInput.value;
      this._panel._loadItemAutoComplete(uri);
    });
    input = p.element.getElementsByTagName("input")[1];
    input._panel = this.panel;
    this.panel._uriInput = input;
    new RefAutoCompleteInput(this.panel._uriInput, this.mapId);
    var form = this.panel.element.getElementsByTagName("form")[0];
    form._panel = this.panel;
    form._itemId = this.itemId;
    new EventListener(form, "submit", function(evn) {
      var a = new InsertItemAction(this._panel._uriInput.value, this._panel._lbInput.value, this._itemId);
      a.suppressNav = !this._ctrlKey;
      a.perform();
      this._panel.close();
      return EventListener.cancelEvent(evn);
    });
    new EventListener(form, "keydown", function(evn) {
      this._ctrlKey = evn.ctrlKey;
    });
	  this.panel._loadItemAutoComplete(this.mapId);
  }
  this.panel.open();
}

ShowInsertAction.prototype.close = function() {
  this.panel._lbInput.focus && this.panel._lbInput.focus();
  this.panel._lbInput.select && this.panel._lbInput.select();
}


function InsertItemAction(mapId, labelValue, itemId) {
  this.id = "insertItem";
  this.label = "Create";
  this.mapId = mapId;
  this.itemId = itemId;
  this.labelValue = labelValue;
}

InsertItemAction.prototype.perform = function() {
  var editJob = new Job(BasePath.get() + MAP_EDIT_JOB);
  editJob.addData("item", this.itemId);
  editJob.addData("map", this.mapId);
  editJob.addData("label", this.labelValue);
  editJob.addData(this.id, "1");
  editJob._action = this;
  editJob.onSuccess = function() {
    if (this.text == "[1]") {
      StatusDisplay.getInstance().show("No changes.", null, true);
      return;
    }
	  var l = Resource.parseJsArray(this.text);
    if (!l) {
      StatusDisplay.getInstance().showError("Failed to create item!", null, true);
      return;
    }
	  var i = l.indexOf("insert");
	  var id = l[i+2];
	  if (this._action.suppressNav || (0 != id.indexOf("item_"))) {
	    id = this._action.itemId;
	    var idx = id.indexOf("#");
	    if (0 <= idx)
	      id = id.substring(idx+1, id.length);
	  }
	  var loc = window.location;
	  var uri = loc.protocol + "//" + loc.host + loc.pathname + "?map=" + l[i+1] + "&item=" + id;
    window.location = uri;
  }
  editJob.start();
}

function InsertContentAction(entityView, tmpl) {
	InsertContentAction._super.call(this, entityView, tmpl);
}
Object.extend("InsertContentAction", "InsertAction");

InsertContentAction.prototype.reselect = function(ev) {
	this.doc.selectionSupport.select(EntityView.getLastChildEntityView(this.doc.contentPanel.element));
	this.doc.editorPanel.open();
}

function InsertNoteAction(entityView) {
  InsertNoteAction._super.call(this, entityView, "/livcos.org/type/template/livcos.org/ns/doc/paragraph#template_1");
}
Object.extend("InsertNoteAction", "InsertContentAction");

function InsertLinkAction(entityView) {
  InsertLinkAction._super.call(this, entityView, "/livcos.org/type/template/livcos.org/ns/doc/link#template_1");
}
Object.extend("InsertLinkAction", "InsertContentAction");

function InsertImageAction(entityView) {
  InsertImageAction._super.call(this, entityView, "/livcos.org/type/template/livcos.org/ns/doc/image#template_1");
}
Object.extend("InsertImageAction", "InsertContentAction");

function InsertImportAction(entityView) {
  InsertImportAction._super.call(this, entityView, "/livcos.org/type/template/livcos.org/ns/doc/import#template_1");
}
Object.extend("InsertImportAction", "InsertContentAction");


function InsertDefaultNoteAction(inlineEdit, entity, content, path) {
  this.id = "insert";
  this.inlineEdit = inlineEdit;
  this.entity = entity;
  this.content = "<doc:paragraph id=\"{new-id()}\" xmlns:doc=\"livcos.org/ns/doc\">" + content + "</doc:paragraph>";
  this.path = path;
}

InsertDefaultNoteAction.prototype.perform = function() {
  var editJob = new Job(BasePath.get() + MAP_EDIT_JOB);
  editJob.addData(this.id, "1");
  editJob.addData("objId", this.entity.objId);
  editJob.addData("id", this.entity.id);
  editJob.addData("ns-m", "livcos.org/ns/map");
  editJob.addData("path", this.path);
  editJob.addData("content", this.content);
  editJob._evDom = this.inlineEdit.dom;
  editJob.onSuccess = function() {
  	var changes = Resource.parseJsArray(this.text);
  	var idx = changes.indexOf("insert");
  	this._evDom.element.id = changes[idx + 1] + "#" + changes[idx + 2];
  	var doc = Doc(this._evDom)
  	doc.addNoteShortcut = null;
    StatusDisplay.getInstance().show("...saved.", null, true);
		doc.selectionSupport.select(this._evDom.getEntityView());
  }
  editJob.start();
}

InsertDefaultNoteAction.prototype.close = function() {
  Doc().selectionSupport.activate(false);
}

function ReorderAction(relId, group, nextId) {
  this.id = "reorder";
  this.label = "Reorder";
  this.relId = relId;
  this.group = group;
  this.nextId = nextId;
}

ReorderAction.prototype.perform = function() {
  var editJob = new Job(BasePath.get() + MAP_EDIT_JOB);
  editJob.addData(this.id, "1");
  var uri = new Uri(this.relId);
  editJob.addData("map", uri.getPath());
  editJob.addData("rel", uri.getFragment());
  editJob.addData("group", this.group);
  if (this.nextId) {
	  uri = new Uri(this.nextId);
	  editJob.addData("next", uri.getFragment());
  }
  editJob.onSuccess = function() {
    StatusDisplay.getInstance().show("...saved.", null, true);
  }
  editJob.start();
}

ReorderAction.prototype.close = function() {
}

function MoveIntoAction(entityView, relation, copy) {
  MoveIntoAction._super.call(this, 
    (copy)?"copyInto":"moveInto", (copy)?"Copy Into":"Move Into", entityView, MAP_EDIT_JOB);
  this.rel = relation;
}
Object.extend("MoveIntoAction", "EditAction");

MoveIntoAction.prototype.addData = function(editJob) {
  MoveIntoAction._super.prototype.addData.call(this, editJob);
  editJob.addData("rel", this.rel.id);
}

